<?php
	require_once(User.php);
	
	interface GiDatabase
	{
		protected $connection;
		public function openConnection(GiUser $user);
		public function closeConnection();
		public function createDatabase($name);
		public function selectDatabase($name);
		public function query($sql);
	}
	
	class GcMySqlDatabase implements GiDatabase
	{
		public function openConnection(GiUser $user)
		{
			$this->connection = mysql_connect($user->serverAddress, $user->name,
			 $user->password);
			 
			return $this->connection;
		}
		
		public function closeConnection()
		{
			if(isset($this->connection))
			{
				mysql_close($this->connection);
				unset($this->connection);
			}
		}
		
		public function createDatabase($name)
		{			
			$query = "CREATE DATABASE ";		
			$query .= $name;
			$result_set = query($query);
			return $result_set;
		}
	
		public function selectDatabase( $name )
		{
			$selectDB = mysql_select_db($name, $this->connection);
			return $selectDB;
		}
		
		public function query($sql)
		{
			$result = mysql_query($sql, $this->connection);
			$this->confirmQuery($result);
			if (!$result)
				die("Database query failed: " . mysql_error());
			return $result;
		}
	}
?>